package com.tech.zhidengxian.util;

import com.qcloud.cos.exception.CosServiceException;
import com.tech.zhidengxian.config.cos.MyCosClient;
import com.tech.zhidengxian.model.dto.CosDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Component
@Slf4j
public class FileUtil {
    private static MyCosClient myCosClient;
    private static String bucketName;

    @Value("${tencent.cloud.oss.bucketName}")
    public void setBucketName(String bucketName) {
        FileUtil.bucketName = bucketName;
    }

    @Resource
    public void setMyCosClient(MyCosClient myCosClient){
        FileUtil.myCosClient = myCosClient;
    }
    private static String originPath;
    @Value("${file.origin.path}")
    public void setOriginPath(String originPath){
        FileUtil.originPath = originPath;
    }

    private static String originUrl;
    @Value("${file.origin.url}")
    public void setOriginUrl(String originUrl){
        FileUtil.originUrl = originUrl;
    }
    public static CosDTO uploadImg(MultipartFile file, CosDTO cosDTO){
        String preFileName = MockUtil.randomUUID();
        int indexOf = cosDTO.getUploadName().lastIndexOf(".");
        String fileName =indexOf==-1? preFileName+ ".png":preFileName + cosDTO.getUploadName().substring(indexOf);
        try {
            myCosClient.putFile(fileName,fileName,file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = myCosClient.getUrl(fileName);
        cosDTO.setName(fileName);
        cosDTO.setPath(bucketName);
        cosDTO.setUrl(url);
//        try {
//            String preFileName = MockUtil.randomUUID();
//            int indexOf = ossDTO.getUploadName().lastIndexOf(".");
//            String fileName = indexOf==-1? preFileName+ ".png" :preFileName+ ossDTO.getUploadName().substring(indexOf);
//            String path = originPath+ossDTO.getFileBizType()+"/"+fileName;
//            File ossFile = new File(path);
//            if (!ossFile.exists()){
//                ossFile.mkdirs();
//            }
//            file.transferTo(ossFile);
//            ossDTO.setName(fileName);
//            ossDTO.setPath(path);
//            ossDTO.setUrl(originUrl+ossDTO.getFileBizType()+"/"+fileName);
//        } catch (IOException e) {
//            log.error("上传失败");
//            throw new RuntimeException(e);
//        }
        return cosDTO;
    }

    public static CosDTO uploadLocalFileImg(File file, CosDTO cosDTO) {
        String fileName;
        if (cosDTO.getName()!= null){
            fileName = cosDTO.getName();
        }else {
            String preFileName = MockUtil.randomUUID();
            int indexOf = file.getName().lastIndexOf(".");
            fileName = indexOf == -1 ? preFileName + ".png" : preFileName + file.getName().substring(indexOf);
        }
        try {
            myCosClient.putLocalFile(fileName,file);
            String url = myCosClient.getUrl(fileName);
            cosDTO.setName(fileName);
            cosDTO.setPath(bucketName);
            cosDTO.setUrl(url);
        } catch (CosServiceException e) {
            e.printStackTrace();
        }
        return cosDTO;
    }

    public static CosDTO uploadLocalInputStreamImg(InputStream inputStream, String fileName,CosDTO cosDTO) {
        try {
            myCosClient.putFile(fileName,fileName,inputStream);
            String url = myCosClient.getUrl(fileName);
            cosDTO.setName(fileName);
            cosDTO.setPath(bucketName);
            cosDTO.setUrl(url);
            return cosDTO;
        } catch (CosServiceException e) {
            e.printStackTrace();
        }
        return null;
    }




}
